package com.xm.controller;

import com.xm.pojo.R;
import com.xm.pojo.comments;
import com.xm.pojo.order;
import com.xm.service.commentsService;
import com.xm.service.orderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Comments")
@CrossOrigin
public class commentsController {

    @Autowired
    private commentsService commentsService;
    @Autowired
    private orderService orderService;


    /*评论详情1*/
    @GetMapping("/comments/{productId}")
    public R orderInfoAll(@PathVariable int productId){
        List<comments> list= commentsService.commentsInfo(productId);
        if(list.size()>0){
            return R.OK().errorMsg("评论展示成功!").data("orderList",list);
        }else{
            return R.ERROR().errorMsg("暂无评论!");
        }
    }



    /*新增评论
    * comments（
    * userId 用户id
    * productId 商品id
    * content  评论
    * stars  星级（1-5星）
    *   ）
    *
    * orderNum    订单号
    * */
    @PostMapping("/addComment/{orderNum}")
    public R orderInfoAll(@PathVariable String orderNum,@RequestBody comments comments){
        boolean falg= commentsService.addComment(comments);
        if(falg){
            boolean statusFlag=orderService.updateStatus(4,orderNum);
            if(statusFlag){
                return R.OK().errorMsg("评论成功!").data("comment",comments);
            }else{
                return R.ERROR().errorMsg("评论失败!");
            }
        }else{
            return R.ERROR().errorMsg("评论失败!");
        }
    }

    /*我的评论*/
    @GetMapping("/userComments/{userId}")
    public R userComment(@PathVariable int userId){
        List<comments> list= commentsService.userComments(userId);
        if(list.size()>0){
            return R.OK().errorMsg("个人评论展示成功!").data("orderList",list);
        }else{
            return R.ERROR().errorMsg("暂无评论!");
        }
    }

    /*删除个人评论（单个）*/
    @DeleteMapping("/userComments/{id}")
    public R deleteComment(@PathVariable int id){
        boolean flag= commentsService.delComment(id);
        if(flag){
            return R.OK().errorMsg("删除成功!");
        }else{
            return R.ERROR().errorMsg("删除失败!");
        }
    }

    /*删除个人评论（多个）*/
    @DeleteMapping("/userComments")
    public R deleteComment(@PathVariable int[] ids){
        boolean flags=true;
        for(int i=0;i<ids.length;i++){
            boolean flag= commentsService.delComment(ids[i]);
            if(!flag){
                flags=false;
            }
        }
        if(flags){
            return R.OK().errorMsg("删除成功!");
        }else{
            return R.ERROR().errorMsg("删除失败!");
        }
    }
    /*删除个人评论（全部）*/
    @DeleteMapping("/userComments/{userId}")
    public R deleteCommentAll(@PathVariable int userId){
        boolean flag= commentsService.delCommentAll(userId);
        if(flag){
            return R.OK().errorMsg("删除成功!");
        }else{
            return R.ERROR().errorMsg("删除失败!");
        }
    }



}
